Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

489
Visualizações
How to fix React Hook "useAppContext" cannot be called in a class component Error?

I want to use useContext() inside componentDidMount() function because I need Id Partition inside it but it gave me error :
React Hook "useAppContext" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function ?

 class Invhom extends Component {
   
            //...
    
     componentDidMount(){
                
             const {IdPartition,setIdPartition } = useAppContext();  // ... Error
            
                    let value = this.IdPartition;
                  //  console.log(value);
           axios.get('http://localhost:9091/inventaire/select_inventaire');
                  } }
            //...         
            }

//------------------------------context.js----------------------------------------


    export const AppContext = createContext(null);



    export function useAppContext() {
      return useContext(AppContext);
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Please see https://reactjs.org/docs/hooks-rules.html :

Only Call Hooks from React Functions

You can't call Hooks from class components.

As noted in the FAQ, you do not have to rewrite your class components. However, please consider using function components and Hooks for new code.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda